﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using BLFacade.entities;
using BLFacade;

namespace ProjectUI
{
    public partial class CustomerContactForm : Form
    {
        private bool passchanged = false;
        private ICustomerContacts contact;
        private Regex emailTest = new Regex(@"^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$");
        public CustomerContactForm()
        {
            InitializeComponent();
            if (FacadeHolder.UserFacade.IsAdministrator()) cmbxCustomer.Enabled = true;
        }

        public CustomerContactForm(ICustomerContacts contact)
        {
            InitializeComponent();
            this.contact = contact;
            if (FacadeHolder.UserFacade.IsAdministrator()) cmbxCustomer.Enabled = true;
            grbxInfo.Enabled = false;
            grbxCredentials.Enabled = false;
            txtFirstName.Text = contact.FirstName;
            txtMiddleName.Text = contact.MiddleName;
            txtLastName.Text = contact.LastName;
            txtEmail.Text = contact.Email;
            txtPhoneNumber.Text = contact.PhoneNumber;
            txtType.Text = contact.Type;
            txtUsername.Text = contact.Credentials.Username;
            cmbxCustomer.DataSource = FacadeHolder.DataFacade.GetCustomers();
            cmbxCustomer.SelectedItem = contact.Customer;
            btnCreate.Text = "Submit";
            btnCreate.Enabled = false;
            this.Text = String.Format("Information about {0}", contact.FullName);
        }

        public CustomerContactForm(ICustomer customer)
        {
            InitializeComponent();
            cmbxCustomer.DataSource = FacadeHolder.DataFacade.GetCustomers();
            cmbxCustomer.SelectedItem = customer;
            grbxInfo.Enabled = true;
            grbxCredentials.Enabled = true;
            flowPanelButtons.Controls.Remove(btnEdit);
            this.Text = String.Format("New contact for {0}", customer.Name);
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            btnCreate.Enabled = grbxCredentials.Enabled = grbxInfo.Enabled = !grbxInfo.Enabled;
        }

        private void txtPassword_TextChanged(object sender, EventArgs e)
        {
            passchanged = true;
        }

        private void txt_Validating(object sender, CancelEventArgs e)
        {
            if (String.IsNullOrWhiteSpace(((TextBox)sender).Text))
            {
                errorFormProvider.SetError((TextBox)sender, "This field mustn't be empty");
            }
            else
            {
                errorFormProvider.SetError((TextBox)sender, "");
            }
        }

        private void txtUsername_Validating(object sender, CancelEventArgs e)
        {
            if (String.IsNullOrWhiteSpace(txtUsername.Text))
            {
                errorFormProvider.SetError(txtUsername, "This field mustn't be empty");
            }
            else if (FacadeHolder.DataFacade.IsUniqueUsername(txtUsername.Text))
            {
                errorFormProvider.SetError(txtUsername, "Username must be unique");
            }
            else
            {
                errorFormProvider.SetError((TextBox)sender, "");
            }
        }

        private void txtPassword_Validating(object sender, CancelEventArgs e)
        {
            if (txtPassword.Text.Length < 8)
            {
                errorFormProvider.SetError(txtPassword, "Password must contain a minimum of 8 characters");
            }
            else
            {
                errorFormProvider.SetError(txtPassword, "");
            }
        }

        private void txtEmail_Validating(object sender, CancelEventArgs e)
        {
            if (!emailTest.IsMatch(txtEmail.Text))
            {
                errorFormProvider.SetError(txtEmail, "Incorrect e-mail");
            }
            else
            {
                errorFormProvider.SetError(txtEmail, "");
            }
        }

        private bool IsValid()
        {
            foreach (Control c in grbxInfo.Controls)
            {
                if (!String.IsNullOrEmpty(errorFormProvider.GetError(c))) return false;
            }
            foreach (Control c in grbxCredentials.Controls)
            {
                if (!String.IsNullOrEmpty(errorFormProvider.GetError(c))) return false;
            }
            return true;
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            if (!IsValid()) return;
            if (btnCreate.Text.Equals("Create"))
            {
                ((ICustomer)cmbxCustomer.SelectedItem).CreateContact(txtFirstName.Text, txtMiddleName.Text, txtLastName.Text,
                    txtEmail.Text, txtPhoneNumber.Text, txtType.Text, txtUsername.Text, txtPassword.Text);
            }
            if (btnCreate.Text.Equals("Submit"))
            {
                contact.FirstName = txtFirstName.Text;
                contact.MiddleName = txtMiddleName.Text;
                contact.LastName = txtLastName.Text;
                contact.Email = txtEmail.Text;
                contact.PhoneNumber = txtPhoneNumber.Text;
                contact.Type = txtType.Text;
                contact.Credentials.Username = txtUsername.Text;
                if (passchanged) contact.Credentials.Password = txtPassword.Text;
            }
            FacadeHolder.DataFacade.SaveChanges();
            this.Close();
        }
    }
}
